package cn.springboot.pioneermall.robotChat.service.Impl;


import cn.springboot.pioneermall.dao.HsChatAskAnswerTemplateMapper;
import cn.springboot.pioneermall.dao.MallUserTokenMapper;
import cn.springboot.pioneermall.robotChat.domain.HsChatAskAnswerTemplate;
import cn.springboot.pioneermall.robotChat.domain.HsChatMessage;
import cn.springboot.pioneermall.robotChat.domain.SysUser;
import cn.springboot.pioneermall.robotChat.service.IHsChatAskAnswerTemplateService;
import cn.springboot.pioneermall.robotChat.service.IHsChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 客服问答模板Service业务层处理
 *
 * @author ljxl
 * @date 2023-10-21
 */
@Service
public class HsChatAskAnswerTemplateServiceImpl implements IHsChatAskAnswerTemplateService {
    @Autowired
    private HsChatAskAnswerTemplateMapper hsChatAskAnswerTemplateMapper;

    @Autowired
    private IHsChatMessageService hsChatMessageService;



    private static final String KF_USER_ID = "1";
    private static final String KF_USER_NAME = "admin";

    /**
     * 查询客服问答模板
     *
     * @param id 客服问答模板ID
     * @return 客服问答模板
     */
    @Override
    public HsChatAskAnswerTemplate selectHsChatAskAnswerTemplateById(String id) {
        return hsChatAskAnswerTemplateMapper.selectHsChatAskAnswerTemplateById(id);
    }

    /**
     * 查询客服问答模板列表
     *
     * @param hsChatAskAnswerTemplate 客服问答模板
     * @return 客服问答模板
     */
    @Override
    public List<HsChatAskAnswerTemplate> selectHsChatAskAnswerTemplateList(HsChatAskAnswerTemplate hsChatAskAnswerTemplate) {
        return hsChatAskAnswerTemplateMapper.selectHsChatAskAnswerTemplateList(hsChatAskAnswerTemplate);
    }

    /**
     * 新增客服问答模板
     *
     * @param hsChatAskAnswerTemplate 客服问答模板
     * @return 结果
     */
    @Override
    public int insertHsChatAskAnswerTemplate(HsChatAskAnswerTemplate hsChatAskAnswerTemplate) {
        return hsChatAskAnswerTemplateMapper.insertHsChatAskAnswerTemplate(hsChatAskAnswerTemplate);
    }

    /**
     * 修改客服问答模板
     *
     * @param hsChatAskAnswerTemplate 客服问答模板
     * @return 结果
     */
    @Override
    public int updateHsChatAskAnswerTemplate(HsChatAskAnswerTemplate hsChatAskAnswerTemplate) {
        return hsChatAskAnswerTemplateMapper.updateHsChatAskAnswerTemplate(hsChatAskAnswerTemplate);
    }

    /**
     * 批量删除客服问答模板
     *
     * @param ids 需要删除的客服问答模板ID
     * @return 结果
     */
    @Override
    public int deleteHsChatAskAnswerTemplateByIds(String[] ids) {
        return hsChatAskAnswerTemplateMapper.deleteHsChatAskAnswerTemplateByIds(ids);
    }

    /**
     * 删除客服问答模板信息
     *
     * @param id 客服问答模板ID
     * @return 结果
     */
    @Override
    public int deleteHsChatAskAnswerTemplateById(String id) {
        return hsChatAskAnswerTemplateMapper.deleteHsChatAskAnswerTemplateById(id);
    }


    @Override
    public String importHsAskAnswerTemplate(List<HsChatAskAnswerTemplate> list) {
        int insertNumber = 0;
        int updateNumber = 0;
        for (HsChatAskAnswerTemplate hsChatAskAnswerTemplate : list) {
            List<HsChatAskAnswerTemplate> hsChatAskAnswerTemplates = selectHsChatAskAnswerTemplateList(hsChatAskAnswerTemplate);
            if (!hsChatAskAnswerTemplates.isEmpty()) {
                HsChatAskAnswerTemplate originalHsChatAskAnswerTemplate = hsChatAskAnswerTemplates.get(0);
                originalHsChatAskAnswerTemplate.setAnswer(hsChatAskAnswerTemplate.getAnswer());
                hsChatAskAnswerTemplateMapper.updateHsChatAskAnswerTemplate(originalHsChatAskAnswerTemplate);
                updateNumber++;
            } else {
                insertHsChatAskAnswerTemplate(hsChatAskAnswerTemplate);
                insertNumber++;
            }
        }
        return "共计更新" + updateNumber + "条数据," + "插入" + insertNumber +
                "条数据！";

    }

    @Override
    public HsChatMessage getAskAnswer(HsChatAskAnswerTemplate hsChatAskAnswerTemplate,
                                      Long userId,String userName) {
        List<HsChatAskAnswerTemplate> hsChatAskAnswerTemplates = selectHsChatAskAnswerTemplateList(hsChatAskAnswerTemplate);
        HsChatMessage replyMessage = new HsChatMessage();
        replyMessage.setToId(userId);
        replyMessage.setToUserName(userName);
        replyMessage.setFromId(Long.valueOf(KF_USER_ID));
        replyMessage.setFromUserName("机器人");
        replyMessage.setSendTime(new Date());
        if (hsChatAskAnswerTemplates.isEmpty()) {
            replyMessage.setSendContent("抱歉,您的问题暂时无法回复,请点击【转人工】按钮进行转接");
        } else {
            replyMessage.setSendContent(hsChatAskAnswerTemplates.get(0).getAnswer());
        }
        hsChatMessageService.insertHsChatMessage(replyMessage);
        return replyMessage;
    }
}
